<?php
/**
 * 诉讼订单详情模型
 */
declare(strict_types=1);

namespace app\model\crm;

use app\model\BaseModel;

class LawsuitOrder extends BaseModel
{
    protected $name = 'crm_lawsuit_order';
    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'delete_time';
    
    // 软删除
    use \think\model\concern\SoftDelete;
    
    // JSON字段
    protected $json = ['civil_stages', 'criminal_stages', 'litigation_request_elements'];
    
    /**
     * 诉讼订单类型常量
     */
    const TYPE_ENTRUST = 1;   // 案件委托
    const TYPE_GUIDANCE = 2;  // 诉讼指导
    
    /**
     * 委托方身份常量
     */
    const PARTY_PLAINTIFF = 1;  // 原告
    const PARTY_DEFENDANT = 2;  // 被告
    
    /**
     * 代理阶段类别常量
     */
    const STAGE_CATEGORY_CIVIL = 1;     // 民商阶段
    const STAGE_CATEGORY_CRIMINAL = 2;  // 刑事阶段
    
    /**
     * 涉案标的金额范围常量
     */
    const AMOUNT_RANGE_UNDER_100K = 1;    // 10万以下
    const AMOUNT_RANGE_100K_500K = 2;     // 10-50万
    const AMOUNT_RANGE_500K_1M = 3;       // 50-100万
    const AMOUNT_RANGE_1M_5M = 4;         // 100-500万
    const AMOUNT_RANGE_OVER_5M = 5;       // 500万以上
    
    /**
     * 关联订单主表
     */
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id', 'id');
    }
    
    /**
     * 关联案由
     */
    public function caseCause()
    {
        return $this->belongsTo(\app\model\lawcase\CaseCause::class, 'case_cause_id', 'id');
    }
    
    /**
     * 获取诉讼类型文本
     */
    public function getLawsuitTypeTextAttr($value, $data): string
    {
        $typeMap = [
            self::TYPE_ENTRUST  => '案件委托',
            self::TYPE_GUIDANCE => '诉讼指导',
        ];
        
        return $typeMap[$data['lawsuit_type']] ?? '未知';
    }
    
    /**
     * 获取委托方身份文本
     */
    public function getEntrustingPartyIdentityTextAttr($value, $data): string
    {
        $identityMap = [
            self::PARTY_PLAINTIFF => '原告',
            self::PARTY_DEFENDANT => '被告',
        ];
        
        return $identityMap[$data['entrusting_party_identity']] ?? '未知';
    }
    
    /**
     * 获取代理阶段类别文本
     */
    public function getLitigationStageCategoryTextAttr($value, $data): string
    {
        $categoryMap = [
            self::STAGE_CATEGORY_CIVIL    => '民商阶段',
            self::STAGE_CATEGORY_CRIMINAL => '刑事阶段',
        ];
        
        return $categoryMap[$data['litigation_stage_category']] ?? '未知';
    }
    
    /**
     * 获取涉案标的金额范围文本
     */
    public function getCaseAmountRangeTextAttr($value, $data): string
    {
        $rangeMap = [
            self::AMOUNT_RANGE_UNDER_100K => '10万以下',
            self::AMOUNT_RANGE_100K_500K  => '10-50万',
            self::AMOUNT_RANGE_500K_1M    => '50-100万',
            self::AMOUNT_RANGE_1M_5M      => '100-500万',
            self::AMOUNT_RANGE_OVER_5M    => '500万以上',
        ];
        
        return $rangeMap[$data['case_amount_range']] ?? '未知';
    }
    
    /**
     * 获取民商阶段文本
     */
    public function getCivilStagesTextAttr($value, $data): array
    {
        $stagesMap = [
            'arbitration'   => '仲裁',
            'first_trial'   => '一审',
            'second_trial'  => '二审',
            'retrial'       => '再审',
            'execution'     => '执行',
        ];
        
        $civilStages = $data['civil_stages'] ?? [];
        if (is_string($civilStages)) {
            $civilStages = json_decode($civilStages, true) ?? [];
        }
        
        $result = [];
        foreach ($civilStages as $stage) {
            if (isset($stagesMap[$stage])) {
                $result[] = $stagesMap[$stage];
            }
        }
        
        return $result;
    }
    
    /**
     * 获取刑事阶段文本
     */
    public function getCriminalStagesTextAttr($value, $data): array
    {
        $stagesMap = [
            'meeting'       => '会见',
            'appeal'        => '上诉',
            'police'        => '公安（侦察）',
            'procuratorate' => '检察院（审查起诉）',
            'court'         => '法院（审判）',
        ];
        
        $criminalStages = $data['criminal_stages'] ?? [];
        if (is_string($criminalStages)) {
            $criminalStages = json_decode($criminalStages, true) ?? [];
        }
        
        $result = [];
        foreach ($criminalStages as $stage) {
            if (isset($stagesMap[$stage])) {
                $result[] = $stagesMap[$stage];
            }
        }
        
        return $result;
    }
    
    /**
     * 获取诉讼请求要素文本
     */
    public function getLitigationRequestElementsTextAttr($value, $data): array
    {
        $elementsMap = [
            'penalty'          => '违约金',
            'interest'         => '利息',
            'litigation_fee'   => '诉讼费',
            'lawyer_fee'       => '律师费',
            'preservation_fee' => '保全费',
        ];
        
        $elements = $data['litigation_request_elements'] ?? [];
        if (is_string($elements)) {
            $elements = json_decode($elements, true) ?? [];
        }
        
        $result = [];
        foreach ($elements as $element) {
            if (isset($elementsMap[$element])) {
                $result[] = $elementsMap[$element];
            }
        }
        
        return $result;
    }
}
